package com.meowcook.controller;

import com.meowcook.common.AppResult;
import com.meowcook.config.AppConfig;
import com.meowcook.pojo.dto.RecipeAddDTO;
import com.meowcook.pojo.dto.RecipeUpdateDTO;
import com.meowcook.pojo.vo.RecipeDetailVO;
import com.meowcook.pojo.vo.RecipePartIVO;
import com.meowcook.pojo.vo.UserPartIVO;
import com.meowcook.service.IRecipeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.List;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 28792
 * Date: 2025-05-06
 * Time: 10:32
 */
@Slf4j
@Api(tags = "食谱操作接口")
@RestController
@RequestMapping("/recipe")
public class RecipeController {
    @Resource
    private IRecipeService recipeService;

    @PostMapping(path = "/add")
    @ApiOperation("添加食谱")
    public AppResult addRecipe(HttpServletRequest request, @Valid @RequestBody RecipeAddDTO recipe) {
        log.info("添加食谱：{}", recipe);
        //获取用户id
        HttpSession session = request.getSession(false);
        UserPartIVO user = (UserPartIVO) session.getAttribute(AppConfig.USER_SESSION_KEY);
        //调用service
        recipeService.createOneRecipe(recipe, user.getId());
        return AppResult.success();
    }


    @GetMapping("/getRecipeList")
    @ApiOperation("获取板块食谱列表")
    public AppResult<List<RecipePartIVO>> getRecipeList(
            @ApiParam("分页大小") @RequestParam("limit") @NonNull Integer limit,
            @ApiParam("分页偏移量") @RequestParam("offset") @NonNull Integer offset,
            @ApiParam("板块id") @RequestParam("boardId") @NonNull Long boardId) {
        log.info("分页大小：{}，分页偏移量：{}，板块id：{}", limit, offset, boardId);
        return AppResult.success(recipeService.selectByBoardId(boardId, offset, limit));
    }

    @ApiOperation("获取用户发布食谱列表")
    @GetMapping("/getRecipeListByUserId")
    public AppResult<List<RecipePartIVO>> getRecipeListByUserId(
            HttpServletRequest httpServletRequest,
            @ApiParam("用户id") @RequestParam(value = "userId", required = false) Long userId,
            @ApiParam("分页大小") @RequestParam("limit") @NonNull Integer limit,
            @ApiParam("分页偏移量") @RequestParam("offset") @NonNull Integer offset) {
        if (userId == null) {
            HttpSession session = httpServletRequest.getSession(false);
            UserPartIVO user = (UserPartIVO) session.getAttribute(AppConfig.USER_SESSION_KEY);
            userId = user.getId();
        }
        log.info("分页大小：{}，分页偏移量：{}，userId：{}", limit, offset, userId);
        List<RecipePartIVO> recipes = recipeService.selectByUserId(userId, offset, limit);
        return AppResult.success(recipes);
    }

    @GetMapping("/getCollectionList")
    @ApiOperation("获取用户收藏食谱列表")
    public AppResult<List<RecipePartIVO>> getCollectionList(HttpServletRequest request,
                                                      @ApiParam("用户id") @RequestParam(value = "userId", required = false) Long userId,
                                                      @ApiParam("收藏位置") @RequestParam(value = "blocked") @NonNull Integer blocked,
                                                      @ApiParam("分页大小") @RequestParam("limit") @NonNull Integer limit,
                                                      @ApiParam("分页偏移量") @RequestParam("offset") @NonNull Integer offset) {
        if (userId == null) {
            HttpSession session = request.getSession(false);
            UserPartIVO userInfo = (UserPartIVO) session.getAttribute(AppConfig.USER_SESSION_KEY);
            userId = userInfo.getId();
        }
        log.info("用户id: {},分页大小：{}，分页偏移量：{}，blocked：{}", userId, limit, offset, blocked);
        List<RecipePartIVO> collections = recipeService.selectCollectionByUserId(userId, offset, limit, blocked);
        return AppResult.success(collections);
    }

    @GetMapping("/getRecipeDetail")
    @ApiOperation("获取食谱详情")
    public AppResult<RecipeDetailVO> getRecipeDetail(@ApiParam("食谱id") @RequestParam("recipeId") @NonNull Long recipeId) {
        log.info("获取食谱id：{}", recipeId);
        return AppResult.success(recipeService.selectDetailByRecipeId(recipeId));
    }

    @ApiOperation("修改食谱")
    @PatchMapping("/update")
    public AppResult updateRecipe(@Valid @RequestBody RecipeUpdateDTO recipeUpdateDTO) {
        log.info("修改食谱：{}", recipeUpdateDTO);
        recipeService.updateRecipe(recipeUpdateDTO);
        return AppResult.success("修改成功");
    }

    @ApiOperation("删除食谱")
    @PostMapping("/delete")
    public AppResult deleteRecipe(@ApiParam("食谱id") @RequestParam("recipeId") @NonNull Long recipeId) {
        log.info("获取食谱ID，{}", recipeId);
        recipeService.deleteRecipe(recipeId);
        return AppResult.success("删除成功");
    }
}
